package com.sto.stoapp.upload;

import java.util.List;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.db.manager.UploadDataManager;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.upload.OrderBackRequest;
import com.sto.stoapp.http.response.upload.OrderDataUploadResponse;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;

/**
 * 订单打回
 * 
 * @author Administrator
 * 
 */
public class UploadOrderBackThread extends BaseThread {

	private Context context;
	private Handler handler;
	private EnumTableNames status;

	public UploadOrderBackThread(Context context, EnumTableNames status,
			Handler handler) {
		super(context);
		this.context = context;
		this.status = status;
		this.handler = handler;
	}

	@Override
	public void run() {
		super.run();
		List<Object> objects = UploadDataManager.getInstance(context)
				.queryUploadData(status);
		if (objects == null || objects.size() == 0) {
			handler.obtainMessage(status.ordinal(), UploadDataService.NO_DATAS)
					.sendToTarget();
			return;
		}
		for (int i = 0; i < objects.size(); i++) {
			Object o = objects.get(i);
			OrderBackRequest orderBackRequest = (OrderBackRequest) o;
			String str = orderBackRequest.buildToRequestString();
			HttpManager mgr = new HttpManager();
			final String response = mgr.doPostClient(str, StoFilePreference
					.getInstance(context).getServerIp()
					+ URLActions.ORDER_REBACK_ACTION);

			Gson gson = new Gson();

			OrderDataUploadResponse orderUploadResponse = null;
			try {
				orderUploadResponse = gson.fromJson(response,
						new TypeToken<OrderDataUploadResponse>() {
						}.getType());
			} catch (JsonSyntaxException e1) {
				e1.printStackTrace();
				ExceptionLog.writeLog("UploadDataService ",
						"UploadOrderBackThread 解析出错：" + response
								+ " /n Exception:" + e1.toString());
				showMsg("上传数据服务器返回数据解析出错");
				handler.obtainMessage(
						EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_BACK
								.ordinal(),
						UploadDataService.ERROR).sendToTarget();
				return;
			}

			if (orderUploadResponse == null) {
				showMsg("上传数据服务器未返回数据");
				ExceptionLog.writeLog("UploadDataService",
						"上传数据服务器未返回数据 orderBackUploadResponse:" + response);
				handler.obtainMessage(
						EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_BACK
								.ordinal(),
						UploadDataService.ERROR).sendToTarget();
				return;
			}
			if ("000".equals(orderUploadResponse.respCode)) {

				showMsg(orderUploadResponse.resMessage);
				// orderGainRequests
				// .remove(orderGainUploadResponse);
			} else {
				// 发送失败..
				UploadDataManager.getInstance(context).getListCode().remove(i);
				ExceptionLog
						.writeLog("UploadOrderBackThread 发送失败request:", str);

				ExceptionLog.writeLog("UploadOrderBackThread 发送失败response:",
						response);
				handler.obtainMessage(
						EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_BACK
								.ordinal(),
						UploadDataService.ERROR).sendToTarget();

			}

		}
		handler.obtainMessage(
				EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_BACK.ordinal(),
				UploadDataService.SUCCESS).sendToTarget();

	}

	private void showMsg(final String text) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				Helper.showToast(context, text);
			}
		});
	}
}
